package com.diqye.templet.regullation;

import java.util.Stack;

import com.diqye.templet.core.Regullation;


/**
 * 字符串替换 查找公式规则
 *例如:
 *asdfasdffdasf
 *iiioaisdf$F_ABS($F_PI())asfasdfs
 *asdf
 *运用此规则会找到$F_ABS($F_PI())
 * @author 秦振龙
 * @date 2013-11-7
 *
 * <b>修改记录及说明：</b>
 * <ul>
 * <li>2013-11-7  秦振龙  创建</li>
 * </ul>
 */
public  class FuncTagRegull implements Regullation {
	public final static String seachStart = "$F_";
	public final static String FUNC_REG = "\\$F_";

	public boolean invok(String context, int[] start, int[] end) {
		int chstart = context.indexOf(seachStart,start[0]);
		if(chstart == -1){
			return false;
		}
		Stack<String> op = new Stack<String>();
		int sindex = chstart+seachStart.length();
		while(true){
			int lk = context.indexOf("(",sindex);
			int rk = context.indexOf(")",sindex);
			if(lk == -1&&op.isEmpty()){
				return false;
			}
			
			if(rk == -1){
				return false;
			}
			if(lk!=-1 &&lk <rk){
				op.push("(");
				sindex = lk+1;
				continue;
			}else{
				op.pop();
				if(op.isEmpty()){
					start[0] = chstart;
					end[0] = rk+1;
					return true;
				}
				sindex = rk+1;
				continue;
			}
		
		}
	}

}